package com.pig4cloud.pig.module.service.location.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.module.api.entity.dto.AreaTreeSelect;
import com.pig4cloud.pig.module.api.entity.location.City;
import com.pig4cloud.pig.module.api.entity.location.District;
import com.pig4cloud.pig.module.api.entity.location.Province;
import com.pig4cloud.pig.module.mapper.location.CityMapper;
import com.pig4cloud.pig.module.mapper.location.DistrictMapper;
import com.pig4cloud.pig.module.mapper.location.ProvinceMapper;
import com.pig4cloud.pig.module.service.location.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 全国的 省份Service业务层处理
 *
 * @author mr.zhou
 * @date 2021-03-13
 */
@Service
public class ProvinceServiceImpl extends ServiceImpl<ProvinceMapper, Province> implements ProvinceService {

    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private DistrictMapper districtMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public List<Province> getProvinceList() {
        List<Province> list = new ArrayList<>();
        boolean blHasKey = redisTemplate.opsForHash().hasKey("provinceList","provinceList");
        if (blHasKey) {
            Object object = redisTemplate.opsForHash().get("provinceList", "provinceList");
            String itemDicts = object != null ? object.toString() : "";
            if (!StrUtil.isBlank(itemDicts)) {
                JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                list = JSONUtil.toList(jsonArray.toString(), Province.class);
            } else {
                list = provinceMapper.selectAllList();
                redisTemplate.opsForHash().put("provinceList", "provinceList", JSONUtil.toJsonStr(list));
            }
        } else {
            list = provinceMapper.selectAllList();
            redisTemplate.opsForHash().put("provinceList", "provinceList", JSONUtil.toJsonStr(list));
        }
        return list;
    }

    @Override
    public List<AreaTreeSelect> getAllArea(Province province){
        List<AreaTreeSelect> list = new ArrayList<>();
        String key =  "areaList_" +province.getId();
        String keyAll = "areaAllList";
        if(province.getIsHasDis() == 0){
			key =  "areaCityList_" +province.getId();
		}
        int isHasDis = province.getIsHasDis();
        if(province.getId()!=null){//只取当前省份的
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (!StrUtil.isBlank(itemDicts)) {
                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                    list = JSONUtil.toList(jsonArray.toString(), AreaTreeSelect.class);
                } else {
                    list = getAllAreaTreeList(province.getId(),isHasDis);
                    redisTemplate.opsForHash().put(key , key, JSONUtil.toJsonStr(list));
                }
            } else {
                list = getAllAreaTreeList(province.getId(),isHasDis);
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(list));
            }
        }else{//取所有省份的
            boolean blHasKey = redisTemplate.opsForHash().hasKey(keyAll,keyAll);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(keyAll, keyAll);
                String itemDicts = object != null ? object.toString() : "";
                if (!StrUtil.isBlank(itemDicts)) {
                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                    list = JSONUtil.toList(jsonArray.toString(), AreaTreeSelect.class);
                } else {
                    list = getAllAreaTreeList(null,isHasDis);
                    redisTemplate.opsForHash().put(keyAll, keyAll, JSONUtil.toJsonStr(list));
                }
            } else {
                list = getAllAreaTreeList(null,isHasDis);
                redisTemplate.opsForHash().put(keyAll, keyAll, JSONUtil.toJsonStr(list));
            }
        }
        return list;
    }

    private List<AreaTreeSelect> getAllAreaTreeList(Integer provinceId,int isHasDis){
        List<AreaTreeSelect> list = new ArrayList<>();
        List<Province> listProvince = new ArrayList<>();
        if(provinceId!=null){
            listProvince = provinceMapper.selectListByColumn("province_id",provinceId);
        }else{
            listProvince = provinceMapper.selectAllList();
        }
        if(CollUtil.isNotEmpty(listProvince)){
            for (Province province : listProvince){

                AreaTreeSelect provinceTree = new AreaTreeSelect();
                provinceTree.setValue(province.getId().toString());
                provinceTree.setLabel(province.getProvinceName());

                List<AreaTreeSelect> cityChilds = new ArrayList<>();

                List<City> cities = cityMapper.selectListByColumn("province_id",province.getId());
                if(CollUtil.isNotEmpty(listProvince)){
                    for (City city : cities){
                        AreaTreeSelect cityTree = new AreaTreeSelect();
                        cityTree.setValue(city.getId().toString());
                        cityTree.setLabel(city.getCityName());
                        cityChilds.add(cityTree);

                        if(isHasDis == 1){
							List<AreaTreeSelect> districtChilds = new ArrayList<>();

							List<District> districts = districtMapper.selectListByColumn("city_id",city.getId());
							if(CollUtil.isNotEmpty(districts)){
								for (District district : districts){
									AreaTreeSelect districtTree = new AreaTreeSelect();
									districtTree.setValue(district.getId().toString());
									districtTree.setLabel(district.getDistrictName());
									districtChilds.add(districtTree);

                                /*List<AreaTreeSelect> townChilds = new ArrayList<>();
                                List<Town> towns = townMapper.selectListByColumn("district_id",district.getDistrictId());
                                districtTree.setChildren(townChilds);*/
								}
							}
							cityTree.setChildren(districtChilds);
						}
                    }
                }
                provinceTree.setChildren(cityChilds);
                list.add(provinceTree);
            }
        }

        return list;
    }

}
